﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using TV_2013.Model;

namespace TV_2013.Handle
{
    class ReadData
    {
        private string fileName;

      //  private List<LessonModel> result = new List<LessonModel>();  

        public ReadData(string _fileName)
        {
            fileName = _fileName;
        }

        //lay du lieu theo thuoc tinh va so luong item con
        public LessonModel GetDataByAttribute(string AttrName, int n)
        {
            var element = XElement.Load(fileName);
            
            LessonModel lessonModel = new LessonModel();

            //lay dc 1 phan tu lesson
            var lesson = from p in element.Elements("Lesson") where (string)p.Attribute("id") == AttrName select p;
            int i = 0;
            lessonModel.ArrayImage = new string[n];
            lessonModel.ArraySound = new string[n];
            foreach (var item in lesson)
            {
                do
                {
                    lessonModel.ArrayImage[i] = item.Element("image" + (i + 1).ToString()).Value;
                    lessonModel.ArraySound[i] = item.Element("sound" + (i + 1).ToString()).Value;
                    i++;
                } while (i<n);
                    
            }

            return lessonModel;
           
        }

        public LessonModel2 GetDataByAttribute2(string AttrName, int n)
        {
            var element = XElement.Load(fileName);

            LessonModel2 lessonModel = new LessonModel2();
            lessonModel.ArraySound = new string[n];
            int i = 0;
            //lay dc 1 phan tu lesson
            var lesson = from p in element.Elements("Lesson") where (string)p.Attribute("id") == AttrName select p;
            foreach (var item in lesson)
            {

                do
                {
                    lessonModel.ArraySound[i] = item.Element("sound" + (i + 1).ToString()).Value;
                    i++;
                } while (i < n);

                lessonModel.TextLeft = item.Element("textLeft").Value;
                lessonModel.ImgLeft = item.Element("imageLeft").Value;
                lessonModel.ContentLeft = item.Element("textContentLeft").Value;
                lessonModel.TextRight = item.Element("textRight").Value;
                lessonModel.ImgRight = item.Element("imageRight").Value;
                lessonModel.ContentRight = item.Element("textContentRight").Value;

            }

            return lessonModel;

        }

        public MiniGameModel GetMiniGameDataByAttribute(string AttrName, int n)
        {
            var element = XElement.Load(fileName);

            MiniGameModel miniGame = new MiniGameModel();
            //lay dc 1 phan tu lesson
            var games = from p in element.Elements("game") where (string)p.Attribute("id") == AttrName select p;
            int i = 0;
            miniGame.ArrayImage = new string[n];
            miniGame.ArrayText = new string[n];
            foreach (var item in games)
            {
                do
                {
                    miniGame.ArrayImage[i] = item.Element("image" + (i + 1).ToString()).Value;
                    miniGame.ArrayText[i] = item.Element("txt" + (i + 1).ToString()).Value;
                    i++;
                } while (i < n);

                miniGame.SoundUrl = item.Element("sound").Value;
                miniGame.Key = item.Element("key").Value;
            }

            return miniGame;

        }

        public CompoundWordModel GetCompoundWordDataByAttribute(string AttrName, int n)
        {
            var element = XElement.Load(fileName);

            CompoundWordModel compoundWordModel = new CompoundWordModel();

            var compoundWords = from p in element.Elements("compoundWord") where (string)p.Attribute("id") == AttrName select p;
            int i = 0;
            compoundWordModel.ArrayText = new string[n];
            foreach (var item in compoundWords)
            {
                do 
                {
                    compoundWordModel.ArrayText[i] = item.Element("txt" + (i + 1).ToString()).Value;
                    ++i;
                } while (i < n);
                compoundWordModel.SoundUrl = item.Element("sound").Value;
            }
            

            return compoundWordModel;
        }

        public DrawTextLetterModel GetDrawTextLetterDataByAttribute(int n)
        {
            var element = XElement.Load(fileName);

            DrawTextLetterModel dTLModel = new DrawTextLetterModel();
            var dtLetters = from p in element.Elements("drawTextLetter") select p;
            int i = 0;
            dTLModel.ArrayText = new string[n];
            foreach (var item in dtLetters)
            {
                do
                {
                    dTLModel.ArrayText[i] = item.Element("txt" + (i + 1).ToString()).Value;
                    ++i;
                } while (i < n);
            }

            return dTLModel;
        }

        public PracticeModel GetPracticeDataByAttribute(string AttrName, int n)
        {
            var element = XElement.Load(fileName);

            PracticeModel practiceModel = new PracticeModel();
            //lay dc 1 phan tu lesson
            var practices = from p in element.Elements("homework") where (string)p.Attribute("id") == AttrName select p;
            int i = 0;
            practiceModel.ArraySound = new string[n];
            foreach (var item in practices)
            {
                do
                {
                    practiceModel.ArraySound[i] = item.Element("sound" + (i + 1).ToString()).Value;
                    i++;
                } while (i < n);

                practiceModel.Title = item.Element("title").Value;
                //practiceModel.ImgURL = item.Element("image").Value;
            }

            return practiceModel;

        }        

        //Lay du lieu tu file xml cho vao list
        //public List<LessonModel> GetData()
        //{

        //    var element = XElement.Load(fileName);

        //    var lesson = from p in element.Elements() select p;

        //    foreach (var _item in lesson)
        //    {
        //        LessonModel item = new LessonModel();
        //        item.Title = _item.Element("Title").Value;
        //        item.ImgLessonUrl = _item.Element("ImageLesson").Value;
        //        item.ImgTitleUrl = _item.Element("ImgTitle").Value;
        //        result.Add(item);
        //    }

        //    return result;
        //}
    }
}
